package csc.approximation;

import csc.main.Colors;
import java.io.Serializable;

/**
 * Class represents plane equation like ax+by+c=z
 * Color pair is stored and can be obtained with getters.
 * @author Stanislau Dubrouski
 */
public class LcaApproximationEquation implements ApproximationEquation, Serializable {

    Colors colorFrom;
    Colors colorTo;

    private double aParam;
    private double bParam;
    private double cParam;

    /**
     *
     * @param colorFrom 1st color of color pair.
     * @param colorTo 2nd color from color pair.
     * @param aParam
     * @param bParam
     * @param cParam
     */
    public LcaApproximationEquation(Colors colorFrom, Colors colorTo, double aParam, double bParam, double cParam) {
        this.colorFrom = colorFrom;
        this.colorTo = colorTo;
        this.aParam = aParam;
        this.bParam = bParam;
        this.cParam = cParam;
    }

    public double getaParam() {
        return aParam;
    }

    public double getbParam() {
        return bParam;
    }

    public double getcParam() {
        return cParam;
    }

    public void setaParam(double aParam) {
        this.aParam = aParam;
    }

    public void setbParam(double bParam) {
        this.bParam = bParam;
    }

    public void setcParam(double cParam) {
        this.cParam = cParam;
    }

    /**
     *
     * @return 1st color of color pair.
     */
    public Colors getColorFrom() {
        return colorFrom;
    }

    /**
     *
     * @return 2nd color of color pair.
     */
    public Colors getColorTo() {
        return colorTo;
    }

    /**
     * Calculates 'z' in ax+by+c=z equation based on given x and y parameters.
     * @param x
     * @param y
     * @return 'z' in ax+by+c=z equation
     */
    public float calculateEstimatedShiftValue(double x, double y) {
        return (float) (x * this.getaParam() + y * this.getbParam() + this.getcParam());
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 97 * hash + (this.colorFrom != null ? this.colorFrom.hashCode() : 0);
        hash = 97 * hash + (this.colorTo != null ? this.colorTo.hashCode() : 0);
        hash = 97 * hash + (int) (Double.doubleToLongBits(this.aParam) ^ (Double.doubleToLongBits(this.aParam) >>> 32));
        hash = 97 * hash + (int) (Double.doubleToLongBits(this.bParam) ^ (Double.doubleToLongBits(this.bParam) >>> 32));
        hash = 97 * hash + (int) (Double.doubleToLongBits(this.cParam) ^ (Double.doubleToLongBits(this.cParam) >>> 32));
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final LcaApproximationEquation other = (LcaApproximationEquation) obj;
        if (this.colorFrom != other.colorFrom) {
            return false;
        }
        if (this.colorTo != other.colorTo) {
            return false;
        }
        if (Double.doubleToLongBits(this.aParam) != Double.doubleToLongBits(other.aParam)) {
            return false;
        }
        if (Double.doubleToLongBits(this.bParam) != Double.doubleToLongBits(other.bParam)) {
            return false;
        }
        if (Double.doubleToLongBits(this.cParam) != Double.doubleToLongBits(other.cParam)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "LcaApproximationEquation{" + "colorFrom: " + this.colorFrom + ", colorTo: " + this.colorTo + ", aParam=" + aParam + ", bParam=" + bParam + ", cParam=" + cParam + '}';
    }

    /**
     * 
     * @param color
     * @return true, if one of the colors equals to given color
     */
    public boolean includesColor(Colors color) {
        return (color.equals(this.colorTo) || color.equals(this.colorFrom));        
    }

}
